#!/bin/bash

# 设置颜色
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

# 检查git
if ! command -v git &> /dev/null; then
    echo -e "${RED}错误: 需要安装git${NC}"
    exit 1
fi

# 检查当前目录
if [ ! -d ".git" ]; then
    echo -e "${RED}错误: 请在项目根目录运行此脚本${NC}"
    exit 1
fi

echo "开始更新..."

# 备份当前配置
echo "备份配置文件..."
./scripts/backup.sh

# 获取最新代码
echo "获取最新代码..."
git fetch origin main

# 检查是否有更新
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse origin/main)

if [ "$LOCAL" = "$REMOTE" ]; then
    echo -e "${GREEN}已是最新版本${NC}"
    exit 0
fi

# 更新代码
echo "更新代码..."
git pull origin main

# 更新依赖
echo "更新依赖..."
source venv/bin/activate
pip install -r requirements.txt
pip install -e .

# 重启服务
if systemctl is-active --quiet ollama-chat; then
    echo "重启服务..."
    sudo systemctl restart ollama-chat
fi

echo -e "${GREEN}更新完成！${NC}" 